package com.scm.controller;

import com.scm.pojo.ScmUser;
import com.scm.service.ScmService;
import com.scm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ScmUserController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/5 19:59
 * @Version 1.0
 **/
@RestController
public class LoginController {
    @Autowired
    private ScmService scmService;


    @RequestMapping("/getDecide")
    @ResponseBody
    public String getDecide(HttpServletRequest request){
        String user = request.getParameter("user");
        List<ScmUser> list = scmService.getAllScmUser().stream().filter(user1 -> user1.getAccount().equals(user)).collect(Collectors.toList());
        if (list.size()==1){
            return "1";
        }else {
            return "0";
        }
    }
    @RequestMapping("login")
    public Result login(HttpServletRequest request, Model model){
        Map map = new HashMap();
        scmService.getAllScmUser().forEach(scmUser -> map.put(scmUser.getAccount(),scmUser.getPassword()));
        String account =request.getParameter("account");
        String password =  request.getParameter("password");
        String name = scmService.getUserName(account);
        if (map.containsKey(account) && map.get(account).equals(password)){
            request.getSession().setAttribute("account",account);

            return Result.ok(name);
        }else {
            return Result.error("用户名或密码错误");
        }
    }
    @RequestMapping("aaa")
    public String aa(){
        return "aa";
    }
}
